package com.dlc.shop.coupon.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.coupon.common.model.CouponProd;
import com.dlc.shop.coupon.common.service.CouponProdService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * @author lgh on 2018/12/27.
 */
@RestController
@RequestMapping("/admin/couponProd")
@Tag(name = "商家端优惠券商品接口")
@AllArgsConstructor
public class CouponProdController {

    private final CouponProdService couponProdService;

    @GetMapping("/page")
    @Operation(summary = "分页获取优惠券商品")
    @PreAuthorize("@pms.hasPermission('admin:couponProd:page')")
    public ServerResponseEntity<IPage<CouponProd>> page(CouponProd couponProd, PageParam<CouponProd> page) {
        IPage<CouponProd> couponProds = couponProdService.page(page, new LambdaQueryWrapper<CouponProd>());
        return ServerResponseEntity.success(couponProds);
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "根据优惠券商品id获取信息")
    @Parameter(name = "id", description = "优惠券商品id" )
    @PreAuthorize("@pms.hasPermission('admin:couponProd:info')")
    public ServerResponseEntity<CouponProd> info(@PathVariable("id") Long id) {
        CouponProd couponProd = couponProdService.getById(id);
        return ServerResponseEntity.success(couponProd);
    }

    @PostMapping
    @Operation(summary = "保存优惠券商品信息")
    @PreAuthorize("@pms.hasPermission('admin:couponProd:save')")
    public ServerResponseEntity<Void> save(@RequestBody @Valid CouponProd couponProd) {
        couponProdService.save(couponProd);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "修改优惠券商品信息")
    @PreAuthorize("@pms.hasPermission('admin:couponProd:update')")
    public ServerResponseEntity<Void> update(@RequestBody @Valid CouponProd couponProd) {
        couponProdService.updateById(couponProd);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除优惠券商品")
    @Parameter(name = "id", description = "优惠券商品id" )
    @PreAuthorize("@pms.hasPermission('admin:couponProd:delete')")
    public ServerResponseEntity<Void> delete(@PathVariable Long id) {
        couponProdService.removeById(id);
        return ServerResponseEntity.success();
    }
}
